package com.cssy.yd.logic.service.impl;

import com.byd.acegiext.domain.User;
import com.cssy.yd.basic.dao.BasicPlaceofstorageDAO;
import com.cssy.yd.logic.dao.LogicPriceDAO;
import com.cssy.yd.logic.dao.LogicPriceDetailsDAO;
import com.cssy.yd.logic.dao.LogicWhAffDAO;
import com.cssy.yd.logic.dao.LogicWhAffstockDAO;
import com.cssy.yd.logic.entity.LogicPriceDO;
import com.cssy.yd.logic.entity.LogicPriceDetailsDO;
import com.cssy.yd.logic.entity.LogicWhAffDO;
import com.cssy.yd.logic.entity.LogicWhAffstockDO;
import com.cssy.yd.logic.service.ILogicWhAffStockService;
import com.cssy.yd.logic.util.MathUtil;
import com.cssy.yd.logic.vo.LogicWhAffstockVO;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.ExcelUtil;
import com.plat.util.IDCodeTypeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("logicWhAffStockService")
public class LogicWhAffStockServiceImpl implements ILogicWhAffStockService{
	@Resource(name = "logicWhAffstockDAO")
	private LogicWhAffstockDAO logicWhAffstockDAO;

	@Resource(name="basicPlaceofstorageDAO")
	private BasicPlaceofstorageDAO basicPlaceofstorageDAO;

	@Resource(name = "logicWhAffDAO")
	private LogicWhAffDAO logicWhAffDAO;

	@Resource(name = "logicPriceDAO")
	private LogicPriceDAO logicPriceDAO;

	@Resource(name = "logicPriceDetailsDAO")
	private LogicPriceDetailsDAO logicPriceDetailsDAO;


	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWhAffstockDO>  list=logicWhAffstockDAO.find(scs);
		rs.setRecordCount(logicWhAffstockDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		Integer i =logicWhAffstockDAO.deleteLogicWhAffstockDOByPrimaryKey(id);
		return null;
	}

	@Override
	public Integer update(LogicWhAffstockDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicWhAffstockDAO.updateLogicWhAffstockDO(t);
	}

	@Override
	public LogicWhAffstockDO save(LogicWhAffstockDO t, User user) throws SyBaseException {
		t.setDcruser(user.getName());
		t.setDctime(new Date());
		logicWhAffstockDAO.insertLogicWhAffstockDO(t);
		return t;
	}

	@Override
	public LogicWhAffstockDO findListById(String id) throws SyBaseException {
		return logicWhAffstockDAO.findLogicWhAffstockDOByPrimaryKey(id);
	}

	/**
	 * 更改库存
	 * @param t 存储有单据信息、物料信息及出入库数量的对象 .
	 * <p>必填项: WhCode，Status，Jquantity，Batch，BillCode，BillType</p>
	 * <p>加权平均时必填：Price,Code,Unit,</p>
	 * @param user 包含当前登录用户信息的对象
	 * @param isUpdateCost 标识是否更改平均单价--加权平均
	 * @return
	 */
	@Override
	public boolean updateWhAffstockQuantity(LogicWhAffDO t, User user, boolean isUpdateCost) throws SyBaseException{

		if(t.getWhCode() == null || "".equals(t.getWhCode())){
			throw new SyBaseException("错误:未填写操作仓库信息!!");
		}
		if(t.getStatus() == null || "".equals(t.getStatus())){
			throw new SyBaseException("错误:未填写物料状态信息!!");
		}
		if(t.getHubCode() == null){
			t.setHubCode("");
		}
		if(t.getJquantity() == null){
			throw new SyBaseException("错误:物料库存更改数量不能为空!!");
		}
		if(t.getBatch() == null){
			throw new SyBaseException("错误:物料批次不能为空!!");
		}
		/**
		 * 需求仓位时开启2013-09-18侯飞
		if(t.getHubCode() == null){
			throw new SyBaseException("错误:物料仓位不能为空!!");
		}
		//验证仓库仓位有无对应关系
		boolean validateRes = basicPlaceofstorageDAO.validateWhCodeAndHubcode(t.getWhCode(), t.getHubCode());
		if(!validateRes){ //bps为空表示仓库与仓位无对应关系，本次未应用仓位所以不做判断
			throw new SyBaseException("错误:仓库" + t.getWhCode() + "与仓位" + t.getHubCode() + "无对应关系!!");
		}
		*/
		//查询是否有物料记录
		SearchConditions scs = new SearchConditions();
		scs.clearCondition();
		scs.addCondition("code", t.getCode(),SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
		scs.addCondition("whcode",t.getWhCode(),SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
		scs.addCondition("hubcode",t.getHubCode(),SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
		scs.addCondition("batch",t.getBatch(),SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
		List<LogicWhAffstockDO> lwdList = logicWhAffstockDAO.find(scs);
		if(lwdList == null || lwdList.size() == 0){  //物料无对应库存信息

			if(t.getQuantity() < 0){ //表示本次为出库操作
				throw new SyBaseException("错误：物料编码为："+t.getCode()+"在指定仓库仓位无库存信息！！");
			}
			//入库操作，新增库存信息
			LogicWhAffstockDO lwd = this.changeLogicWhAffDOToLogicWhAffstockDO(t);
			if("1".equals(t.getStatus())){   //状态1：一等
				lwd.setYdengquantity(t.getJquantity());
			}else if("2".equals(t.getStatus())){  //状态2：次品
				lwd.setQuantity1(t.getJquantity());
			}else if("3".equals(t.getStatus())){  //状态3：旧货
				lwd.setQuantity2(t.getJquantity());
			}else if("4".equals(t.getStatus())){//冻结库存
				lwd.setHgquantity(t.getJquantity());
			}else if("5".equals(t.getStatus())){//在途库存
				lwd.setBhgquantity(t.getJquantity());
			}
			lwd.setId(IDCodeTypeUtil.getId());
			lwd.setDcruser(user.getName());
			lwd.setDctime(new Date());
			this.logicWhAffstockDAO.insertLogicWhAffstockDO(lwd); //新增库存信息
		}else{
			double subQuantity = 0.0; //标识库存足不足
			LogicWhAffstockDO lwd = lwdList.get(0);
			if("1".equals(t.getStatus())){ //状态1：一等
				subQuantity = MathUtil.mathUtil.addtract(t.getJquantity(), lwd.getYdengquantity() == null ? 0.0 : lwd.getYdengquantity());
				lwd.setYdengquantity(subQuantity);
			}else if("2".equals(t.getStatus())){ //状态2：次品
				subQuantity = MathUtil.mathUtil.addtract(t.getJquantity(), lwd.getQuantity1() == null ? 0.0 : lwd.getQuantity1());
				lwd.setQuantity1(subQuantity);
			}else if("3".equals(t.getStatus())){//状态3：旧货
				subQuantity = MathUtil.mathUtil.addtract(t.getJquantity(), lwd.getQuantity2() == null ? 0.0 : lwd.getQuantity2());
			    lwd.setQuantity2(subQuantity);
			}else if("4".equals(t.getStatus())){//冻结库存
				subQuantity = MathUtil.mathUtil.addtract(t.getJquantity(), lwd.getHgquantity() == null ? 0.0 : lwd.getHgquantity());
			    lwd.setHgquantity(subQuantity);
			}else if("5".equals(t.getStatus())){//在途库存
				subQuantity = MathUtil.mathUtil.addtract(t.getJquantity(), lwd.getBhgquantity() == null ? 0.0 : lwd.getBhgquantity());
			    lwd.setBhgquantity(subQuantity);
			}
			if(subQuantity < 0){
				throw new SyBaseException("错误：物料"+t.getCode()+"在指定仓库("+t.getWhCode()+")仓位库存，等级（"+t.getStatus()+"）不足！！缺少数量："+(-subQuantity));
			}
			lwd.setDuuser(user.getName());
			lwd.setDutime(new Date());
			logicWhAffstockDAO.updateLogicWhAffstockDO(lwd);//更新库存
		}
		//更改平均单价
		if (isUpdateCost){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("code", t.getCode());
			map.put("name", t.getName());
			map.put("quantity", t.getJquantity());
			map.put("whCode", t.getWhCode());
			map.put("price", t.getPrice());
			map.put("hubName",t.getBillType());//单据类型2013-09-18侯飞
			map.put("whName", t.getWhname());
			map.put("hubCode", t.getHubCode());
			/**
			 * 需求仓位时开启2013-09-18侯飞
			 *
			map.put("hubName", t.getHubName());//在此hubname被当作单据类型使用2013-09-18侯飞
			*/
			map.put("hcsl", getSumQuantityByWhInfoAndMaInfo(t.getCode(),t.getWhCode())>0?getSumQuantityByWhInfoAndMaInfo(t.getCode(),t.getWhCode()):0.0);
			LogicPriceDO lp = this.updateCostQuaAndPrice(map, user,isUpdateCost);
			t.setPrice(lp.getSumprice());
		}
		t.setId(IDCodeTypeUtil.getId());
		t.setDcruser(user.getName());
		t.setDctime(new Date());
		logicWhAffDAO.insertLogicWhAffDO(t); //插入出入库信息记录
		return true;
	}

	/**
	 * 转换对象
	 * @param t
	 * @return
	 */
	public LogicWhAffstockDO changeLogicWhAffDOToLogicWhAffstockDO(LogicWhAffDO t){
		LogicWhAffstockDO lwd = new LogicWhAffstockDO();
		lwd.setBatch(t.getBatch() == null ? null : t.getBatch());
		lwd.setCode(t.getCode() == null ? null : t.getCode());
		lwd.setHubCode(t.getHubCode() == null ? null : t.getHubCode());
		lwd.setHubName(t.getHubName() == null ? null : t.getHubName());
		lwd.setName(t.getName() == null ? null : t.getName());
		lwd.setPrice(t.getPrice() == null ? null : t.getPrice());
		lwd.setSpec(t.getSpec() == null ? null : t.getSpec());
		lwd.setStatus(t.getStatus() == null ? null : t.getStatus());
		lwd.setStockPrice(t.getStockPrice() == null ? null : t.getStockPrice());
		lwd.setUnit(t.getJunit() == null ? null : t.getJunit());
		lwd.setWhCode(t.getWhCode() == null ? null : t.getWhCode());
		lwd.setWhname(t.getWhname()==null?null:t.getWhname());
		lwd.setBzq(t.getBzq()==null?null:t.getBzq());
		lwd.setScrq(t.getScrq()==null?null:t.getScrq());
		lwd.setYxq(t.getYxq()==null?null:t.getYxq());
		return lwd;
	}

	/**
	 * 修改平均单价及总库存信息
	 * @param map 入库物料信息(需包含物料编码、仓库编码、数量  可选仓位编码、价格)
	 * @param user 登录用户
	 * @return
	 * @throws SyBaseException
	 */
	@Transactional(propagation=Propagation.REQUIRED)//REQUIRED:业务方法需要在一个容器里运行。如果方法运行时，已经处在一个事务中，那么加入到这个事务，否则自己新建一个新的事务。
	public LogicPriceDO updateCostQuaAndPrice(Map map,User user, boolean isUpdate)throws SyBaseException{

		if((Double)map.get("quantity") < 0){ //判断是否是出库，出库不能修改平均价
			throw new SyBaseException("本次单据类型为出库不能修改平均单价！！");
		}

		LogicPriceDO lpdParam = new LogicPriceDO();
		lpdParam.setCode((String)map.get("code"));
		lpdParam.setWhCode(map.get("whCode") == null ? null : (String)map.get("whCode"));
		lpdParam.setHubCode(map.get("hubCode") == null ? null : (String)map.get("hubCode"));
		/**
		 * 需求仓位时开启2013-09-18侯飞
		 *
		lpdParam.setHubName(map.get("hubName") == null ? null : (String)map.get("hubName"));
		*/
		lpdParam.setWhName(map.get("whName") == null ? null : (String)map.get("whName"));
		lpdParam.setHubName(map.get("hubName") == null ? null : (String)map.get("hubName"));//这里用作记录单据类型！如果在需求仓位的情况下这里需要增加一个字段记录单据类型2013-09-18侯飞
		double inPrice = map.get("price") == null ? 0.0 : (Double)map.get("price"); //获取本次入库单价
		double inQuantity = (Double)map.get("quantity"); //获取本次入库数量
		double kcQuantity = (Double)map.get("hcsl"); //获取剩余库存数量
		lpdParam.setPrice(inPrice);//2013-09-18侯飞
		lpdParam.setQuantity(inQuantity);//2013-09-18侯飞
		List<LogicPriceDO> lpdList = logicPriceDAO.findListByExample(lpdParam); //查询平均单价及库存信息
		if(lpdList == null || lpdList.size() == 0){

			lpdParam.setId(IDCodeTypeUtil.getId());
			if(isUpdate){
				lpdParam.setSumprice(inPrice);
			}else{
				lpdParam.setSumprice(0.0);
			}
			lpdParam.setSumquantity(inQuantity);
			lpdParam.setName((String)map.get("name"));
			lpdParam.setDcruser(user.getName());
			lpdParam.setDctime(new Date());
			logicPriceDAO.insertLogicPriceDO(lpdParam);//新增平均单价信息
			this.insertPriceDetails(lpdParam, user);
		}else if( lpdList.size() == 1 ){

			lpdParam = lpdList.get(0);
			//XXX 发生过因存数数值过大，导致的数据库异常
			lpdParam.setSumquantity((kcQuantity-inQuantity) + inQuantity);
			lpdParam.setDuuser(user.getName());
			lpdParam.setDutime(new Date());
			if(isUpdate){
				if(kcQuantity>0){
					if(inPrice>0){
							double avPrice = ((kcQuantity-inQuantity) * lpdParam.getSumprice() + inQuantity * inPrice)
									/ (lpdParam.getSumquantity()); // 计算新的平均单价
							lpdParam.setSumprice(avPrice);
					}
				}else{
					lpdParam.setSumprice(0.0);
					lpdParam.setSumquantity(0.0);
				}
			}
			logicPriceDAO.updateLogicPriceDO(lpdParam); //更新库存信息及平均单价
			this.insertPriceDetails(lpdParam, user);
		}else{
			throw new SyBaseException("物料有多条平均单价信息修改平均单价或库存异常！！");
		}
		return lpdParam;
	}

	@Transactional(propagation=Propagation.REQUIRED)//REQUIRED:业务方法需要在一个容器里运行。如果方法运行时，已经处在一个事务中，那么加入到这个事务，否则自己新建一个新的事务。
	public void insertPriceDetails(LogicPriceDO lp,User user)throws SyBaseException{
		LogicPriceDetailsDO lpd=new LogicPriceDetailsDO();
		lpd.setCode(lp.getCode());
		lpd.setName(lp.getName());
		lpd.setHubCode(lp.getHubCode());
		lpd.setHubName(lp.getHubName());
		lpd.setPrice(lp.getPrice());
		lpd.setWhName(lp.getWhName());
		lpd.setWhCode(lp.getWhCode());
		lpd.setQuantity(lp.getQuantity());
		lpd.setSumquantity(lp.getSumquantity());
		lpd.setSumprice(lp.getSumprice());
		lpd.setId(IDCodeTypeUtil.getId());
		lpd.setDcruser(user.getName());
		lpd.setDctime(new Date());
		logicPriceDetailsDAO.insertLogicPriceDetailsDO(lpd);
	}

	/**
	 * 列表显示库存信息
	 */
	@Override
	public SearchResult findForWareHouseStockByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWhAffstockVO>  list=logicWhAffstockDAO.findForWareHouseStock(scs);
//		DecimalFormat df = new DecimalFormat("#.000");
//		for(LogicWhAffstockVO lwsv : list){
//			lwsv.setSumPrice(Double.parseDouble(df.format(lwsv.getSumPrice())));
//			lwsv.setYdengMoney(Double.parseDouble(df.format(lwsv.getYdengMoney())));
//			lwsv.setQuantity2Money(Double.parseDouble(df.format(lwsv.getQuantity2Money())));
//			lwsv.setQuantity1Money(Double.parseDouble(df.format(lwsv.getQuantity1Money())));
//			lwsv.setHgquantityMoney(Double.parseDouble(df.format(lwsv.getHgquantityMoney())));
//			lwsv.setBhgquantityMoney(Double.parseDouble(df.format(lwsv.getBhgquantityMoney())));
//			lwsv.setSumMoney(lwsv.getYdengMoney() + lwsv.getHgquantityMoney() + lwsv.getQuantity1Money()+lwsv.getQuantity2Money()+lwsv.getBhgquantityMoney());
//		}
		rs.setRecordCount(logicWhAffstockDAO.findForWareHouseStockCount(scs));
		rs.setResults(list);
		return rs;
	}

	/**
	 * 验证库存是否足够用于出库
	 * @param maCode 物料编码
	 * @param whCode 仓库编码
	 * @param hubCode 仓位编码(无仓位参数定义为空)
	 * @param status 物料状态
	 * @param batch  批次(无批次参数定义为空)
	 * @param quantity 本次出库数量
	 * @return
	 */
	@Override
	public boolean validateStorageQuaitity(String maCode , String whCode ,
			String hubCode ,String status, String batch , double quantity) throws SyBaseException{

		SearchConditions scs = new SearchConditions();
		scs.addCondition("code", maCode,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
		scs.addCondition("whcode",whCode,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
		scs.addCondition("hubcode",hubCode,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
		scs.addCondition("batch",batch,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
		SearchResult sr = this.findListByExample(scs);
		if(sr == null || sr.getResults() == null || sr.getResults().size() < 1){
			throw new SyBaseException("错误: 指定物品存储位置无此物品库存信息存在!!");
		}else if(sr != null && sr.getResults() != null && sr.getResults().size() > 1){
			throw new SyBaseException("错误: 指定物品存储位置有多条物品库存信息存在!!");
		}

		LogicWhAffstockDO lwsd = (LogicWhAffstockDO)sr.getResults().get(0);

		double subQuantity = 0.0;
		if("1".equals(status)){ //status= 1 一等
			subQuantity = MathUtil.mathUtil.subtract(lwsd.getYdengquantity() == null ? 0.0 :lwsd.getYdengquantity(), quantity);
		}else if("2".equals(status)){ //status= 2 次品
			subQuantity = MathUtil.mathUtil.subtract(lwsd.getQuantity1() == null ? 0.0 : lwsd.getQuantity1(), quantity);
		}else if("3".equals(status)){ //status= 3旧品
			subQuantity = MathUtil.mathUtil.subtract(lwsd.getQuantity2() == null ? 0.0 : lwsd.getQuantity2(), quantity);
		}else if("4".equals(status)){ //status= 3冻结库存
			subQuantity = MathUtil.mathUtil.subtract(lwsd.getHgquantity() == null ? 0.0 : lwsd.getHgquantity(), quantity);
		}else if("5".equals(status)){ //status= 3在途库存
			subQuantity = MathUtil.mathUtil.subtract(lwsd.getBhgquantity() == null ? 0.0 : lwsd.getBhgquantity(), quantity);
		}

		if( subQuantity < 0 ){
			return false;
		}
		return true;
	}


	@Override
	public List<LogicWhAffstockDO> findwhcode(String s) throws SyBaseException {
		return logicWhAffstockDAO.findwhcode(s);
	}
	@Override
	public Double getStorageQuaitity(String maCode, String whCode,
			String hubCode, String status, String batch)
			throws SyBaseException {
		SearchConditions scs = new SearchConditions();
		scs.addCondition("code", maCode,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-14]
		scs.addCondition("whcode",whCode,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-14]
		scs.addCondition("hubcode",hubCode,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-14]
		scs.addCondition("batch",batch,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-14]
		SearchResult sr = this.findListByExample(scs);
		double subQuantity = 0.0;
		if(sr == null || sr.getResults() == null || sr.getResults().size() < 1){
			return 0.0;
		}else if(sr != null && sr.getResults() != null && sr.getResults().size() > 1){
			throw new SyBaseException("错误:指定位置处有多条库存信息!! 物料编码："+maCode+" 仓库编码："+whCode+" 仓位："+hubCode+"  批次："+batch);
		}
		LogicWhAffstockDO lwsd = (LogicWhAffstockDO)sr.getResults().get(0);
		if("1".equals(status)){ //status= 1 一等
			if (lwsd.getYdengquantity() == null)
				subQuantity = 0.0;
			else
			subQuantity = lwsd.getYdengquantity();
		}else if("2".equals(status)){ //status= 2 次品
			if (lwsd.getQuantity1() == null)
				subQuantity = 0.0;
			else
			subQuantity = lwsd.getQuantity1();
		}else if("3".equals(status)){ //status= 3旧品
			if (lwsd.getQuantity2() == null){
				subQuantity = 0.0;
			}else
			subQuantity = lwsd.getQuantity2();
		}else if("4".equals(status)){ //status= 3冻结库存
			if (lwsd.getHgquantity() == null){
				subQuantity = 0.0;
			}else
			subQuantity = lwsd.getHgquantity();
		}else if("5".equals(status)){ //status= 3在途库存
			if (lwsd.getBhgquantity() == null){
				subQuantity = 0.0;
			}else
			subQuantity = lwsd.getBhgquantity();
		}
		return subQuantity;
	}

	/**
	 * 查询物料在指定仓库中的总数量
	 * @param maCode 指定物料编码
	 * @param whCode 仓库
	 * @return 库存总数
	 */
	@Override
	public double getSumQuantityByWhInfoAndMaInfo(String maCode , String whCode) throws SyBaseException{

		if( maCode == null ){
			throw new SyBaseException("错误:物料编码为空，查询库存信息失败!!");
		}
		if( whCode == null ){
			throw new SyBaseException("错误:仓库编码为空，查询库存信息失败!!");
		}

		SearchConditions scs = new SearchConditions();
		scs.addCondition("code", maCode,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
		scs.addCondition("whcode",whCode,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]

		SearchResult sr = this.findListByExample(scs);

		double sumQuantity = 0.0;
		if(sr != null && sr.getResults() != null && sr.getResults().size() >= 1){

			for(LogicWhAffstockDO lwsd : (List<LogicWhAffstockDO>)sr.getResults()){
				double ydengquantity = lwsd.getYdengquantity() == null ? 0.0 : lwsd.getYdengquantity();//一等
				double quantity1 = lwsd.getQuantity1() == null ? 0.0 : lwsd.getQuantity1();//次品
				double quantity2 = lwsd.getQuantity2() == null ? 0.0 : lwsd.getQuantity2();//旧件
				double hggquantity = lwsd.getHgquantity() == null ? 0.0 : lwsd.getHgquantity();//冻结
				double bhgquantity = lwsd.getBhgquantity() == null ? 0.0 : lwsd.getBhgquantity();//在途
				sumQuantity += ydengquantity + bhgquantity + quantity1+quantity2+hggquantity;
			}
		}
		return sumQuantity;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cssy.yd.logic.service.ILogicWhAffStockService#getSumQuantityByWhInfoAndMaInfoAndStatus(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public double getSumQuantityByWhInfoAndMaInfoAndStatus(String maCode , String whCode,String status) throws SyBaseException{
//		if( maCode == null ){
//			throw new SyBaseException("错误:物料编码为空，查询库存信息失败!!");
//		}
		if( whCode == null ){
			throw new SyBaseException("错误:仓库编码为空，查询库存信息失败!!");
		}
//		if(null == status){
//			throw new SyBaseException("错误:物料等级为空，查询库存信息失败!!");
//		}
//
//		SearchConditions scs = new SearchConditions();
//		scs.addCondition("code", maCode,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
//		scs.addCondition("whcode",whCode,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
//		scs.addCondition("status",status,SearchCondition.OP_EQUAL);//修正查询bug @modify by huang.xuming[2013-09-16]
//
//		SearchResult sr = this.findListByExample(scs);
//
//		double sumQuantity = 0.0;
//		if(sr != null && sr.getResults() != null && sr.getResults().size() > 0){
//
//			for(LogicWhAffstockDO lwsd : (List<LogicWhAffstockDO>)sr.getResults()){
//				double ydengquantity = lwsd.getYdengquantity() == null ? 0.0 : lwsd.getYdengquantity();//一等
//				double quantity1 = lwsd.getQuantity1() == null ? 0.0 : lwsd.getQuantity1();//次品
//				double quantity2 = lwsd.getQuantity2() == null ? 0.0 : lwsd.getQuantity2();//旧件
//				double hggquantity = lwsd.getHgquantity() == null ? 0.0 : lwsd.getHgquantity();//冻结
//				double bhgquantity = lwsd.getBhgquantity() == null ? 0.0 : lwsd.getBhgquantity();//在途
//				sumQuantity += ydengquantity + bhgquantity + quantity1+quantity2+hggquantity;
//			}
//		}
//
//		return sumQuantity;

		//@modify huang.xuming [2013-10-07]
		return getSumQuantity( maCode , status,  whCode, null);
	}

	@Override
	public double getSumQuantityByMaInfoAndStatus(String maCode , String status) throws SyBaseException{
		return getSumQuantity( maCode , status,  null, null);
	}

	/**
	 * 查询物料的数量
	 * @modify huang.xuming [2013-10-07]
	 * @param maCode
	 * @param status
	 * @param whCode
	 * @param hubCode
	 * @return
	 * @throws SyBaseException
	 */
	private double getSumQuantity(String maCode ,String status, String whCode,String hubCode) throws SyBaseException{
		SearchConditions scs = new SearchConditions();

		if( maCode == null ){
			throw new SyBaseException("错误:物料编码为空，查询库存信息失败!!");
		}
		if(null == status){
			throw new SyBaseException("错误:物料等级为空，查询库存信息失败!!");
		}
		if( whCode == null ){
			scs.addCondition("whcode",whCode,SearchCondition.OP_EQUAL);
		}
		if( whCode == null ){
			scs.addCondition("hubcode",hubCode,SearchCondition.OP_EQUAL);
		}
		scs.addCondition("code", maCode,SearchCondition.OP_EQUAL);
		scs.addCondition("status",status,SearchCondition.OP_EQUAL);

		SearchResult sr = this.findListByExample(scs);

		double sumQuantity = 0.0;
		if(sr != null && sr.getResults() != null && sr.getResults().size() > 0){

			for(LogicWhAffstockDO lwsd : (List<LogicWhAffstockDO>)sr.getResults()){
				double ydengquantity = lwsd.getYdengquantity() == null ? 0.0 : lwsd.getYdengquantity();//一等
				double quantity1 = lwsd.getQuantity1() == null ? 0.0 : lwsd.getQuantity1();//次品
				double quantity2 = lwsd.getQuantity2() == null ? 0.0 : lwsd.getQuantity2();//旧件
				double hggquantity = lwsd.getHgquantity() == null ? 0.0 : lwsd.getHgquantity();//冻结
				double bhgquantity = lwsd.getBhgquantity() == null ? 0.0 : lwsd.getBhgquantity();//在途
				sumQuantity += ydengquantity + bhgquantity + quantity1+quantity2+hggquantity;
			}
		}

		return sumQuantity;
	}

	@Override
	public void exportExcel(SearchConditions scs, HttpServletResponse response, boolean isShowMoney) throws SyBaseException {

		SearchResult result = this.findForWareHouseStockByExample(scs);
		List<LogicWhAffstockVO> list = result.getResults();
		for(LogicWhAffstockVO lwaffsv : list){
			if(lwaffsv.getYdengquantity() == null){
				lwaffsv.setYdengquantity(0.0);
			}
			if(lwaffsv.getQuantity1() == null){
				lwaffsv.setQuantity1(0.0);
			}
			if(lwaffsv.getQuantity2() == null){
				lwaffsv.setQuantity2(0.0);
			}
			if(lwaffsv.getHgquantity() == null){
				lwaffsv.setHgquantity(0.0);
			}
			if(lwaffsv.getBhgquantity() == null){
				lwaffsv.setBhgquantity(0.0);
			}
		}

		String row = "物料编码:code┋物料名称:name┋物料类型:maType┋型号:spec┋单位:unit┋仓库:whName┋批次:batch┋总库存:sumQuantity┋一等数量:ydengquantity┋次品数量:quantity1┋旧件数量:quantity2┋冻结数量:hgquantity┋在途数量:bhgquantity";
//		if(isShowMoney){
//			row = "所属区域:area┋物料类型:maType┋物料编号:code┋物料名称:name┋型号:spec┋单位:unit┋仓库:whName┋仓位:hubName┋总库存:sumQuantity┋一等数量:ydengquantity┋次品数量:quantity1┋旧件数量:quantity2┋平均单价:sumPrice┋一等库存金额:ydengMoney┋次品库存金额:bhgMoney┋旧件库存金额:quantity1Money┋总金额:sumMoney";
//		}
		ExcelUtil.toExcel(list,row,"库存列表导出",response);
	}

	@Override
	public List<String> findbatch(Map map) throws SyBaseException {

		return logicWhAffstockDAO.findbatch(map);
	}

	@Override
	public SearchResult stockGather(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWhAffstockDO>  list=logicWhAffstockDAO.stockGather(scs);
		rs.setRecordCount(logicWhAffstockDAO.stockGatherCount(scs));
		rs.setResults(list);
		return rs;
	}
	@Override
	public List<LogicWhAffstockDO> findMaterialsByBatchAll(Map<String, Object> map) throws SyBaseException {
		return logicWhAffstockDAO.findByBatchAll(map);
	}
	@Override
	public List<LogicWhAffstockDO> findMaterialsBySearchBatch(SearchConditions scs) throws SyBaseException {
		return logicWhAffstockDAO.findBySearchBatch(scs);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}
}
